package com.xd.hbsys.ps;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class PSService {

    @Resource(name = "hbJdbcTemplate")
    private JdbcTemplate hbJdbcTemplate;

    @Resource
    private PSDao psDao;

    public String queryallps() {
        String sql = "select PSCODE,PSNAME,CITYNAME,LONGITUDE,LATITUDE,PSCLASSNAME from PSBASEINFO";
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();
        JSONArray arr = new JSONArray();
        while (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            String name = (String) r.get("PSNAME");
            String code = (String) r.get("PSCODE");
            float lon = (float) r.get("LONGITUDE");
            float lat = (float) r.get("LATITUDE");
            String psclass = (String) r.get("PSCLASSNAME");
            String city = (String) r.get("CITYNAME");

            JSONObject obj = new JSONObject();
            obj.put("psname", name);
            obj.put("pscode", code);
            obj.put("lon", lon);
            obj.put("lat", lat);
            obj.put("psclass", psclass);
            obj.put("city", city);
            arr.add(obj);
        }
        return arr.toJSONString();
    }

    public String queryps(String ps) {
      /*  String sql = "select * from PSBASEINFO where PSCODE = \'" + ps + "\'";
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);*/

        List<Map<String, Object>> rows = psDao.queryForList(ps);
        Iterator it = rows.iterator();
        JSONObject obj = new JSONObject();
        while (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            String psname = (String) r.get("PSNAME");
            String pscode = (String) r.get("PSCODE");
            float lon = (float) r.get("LONGITUDE");
            float lat = (float) r.get("LATITUDE");

            String psclass = (String) r.get("PSCLASSNAME");
            String attention = (String) r.get("ATTENTIONDEGREENAME");
            String industry = (String) r.get("INDUSTRYTYPENAME");
            String unit = (String) r.get("UNITTYPENAME");
            String region = (String) r.get("REGIONNAME");
            String pollutants = (String) r.get("MAINPOLLUTANTS");
            String city = (String) r.get("CITYNAME");
            String if30 = (String) r.get("IFTHIRTYTENTHOUSANDKILOWAT");
            float area = (float) r.get("TOTALAREA");

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

    	/*	//烟尘
    		List<Integer> l1 =  Arrays.asList(0,0,0,0,0,0,0);
    		//二氧化硫
    		List<Integer> l2 =  Arrays.asList(0,0,0,0,0,0,0);
    		//氮化物
    		List<Integer> l3 =  Arrays.asList(0,0,0,0,0,0,0);*/

            //烟尘
            List l1 = new ArrayList<>();
            //二氧化硫
            List l2 = new ArrayList<>();
            //氮化物
            List l3 = new ArrayList<>();

            //最近一周气体排放量(001烟尘，002二氧化硫，003氮氧化物)
            /*   List<Map<String, Object>> map = psDao.findGasDayPFL_Week();*/

            List<Map<String, Object>> map1 = psDao.findGasDayPFL_Week001(ps);
            List<Map<String, Object>> map2 = psDao.findGasDayPFL_Week002(ps);
            List<Map<String, Object>> map3 = psDao.findGasDayPFL_Week003(ps);

            List<Map<String, Object>> map4 = new ArrayList<>();

            if (map1.size() != 7) {
                Map<String, Object> map1_1 = new HashMap<>();
                map1_1.put("PSCODE", ps);
                map1_1.put("POLLUTANTCODE", "001");
                map1_1.put("REVISEDFLOW", 0.0);
                for (int i = 0; i < 7; i++) {
                    map1.add(map1_1);
                }
            }

            if (map2.size() != 7) {
                Map<String, Object> map1_2 = new HashMap<>();
                map1_2.put("PSCODE", ps);
                map1_2.put("POLLUTANTCODE", "002");
                map1_2.put("REVISEDFLOW", 0.0);
                for (int i = 0; i < 7; i++) {
                    map2.add(map1_2);
                }
            }

            if (map3.size() != 7) {
                Map<String, Object> map1_3 = new HashMap<>();
                map1_3.put("PSCODE", ps);
                map1_3.put("POLLUTANTCODE", "003");
                map1_3.put("REVISEDFLOW", 0.0);
                for (int i = 0; i < 7; i++) {
                    map3.add(map1_3);
                }
            }


            map4.addAll(map1);
            map4.addAll(map2);
            map4.addAll(map3);
           /* for (Map<String, Object> stringObjectMap : map4) {


            }*/

            for (Map<String, Object> stringObjectMap : map4) {


                if (stringObjectMap.get("POLLUTANTCODE").equals("001") || stringObjectMap.get("POLLUTANTCODE") == "001") {
                    l1.add(stringObjectMap.get("REVISEDFLOW"));
                } else if (stringObjectMap.get("POLLUTANTCODE").equals("002") || stringObjectMap.get("POLLUTANTCODE") == "002") {
                    l2.add(stringObjectMap.get("REVISEDFLOW"));
                } else if (stringObjectMap.get("POLLUTANTCODE").equals("003") || stringObjectMap.get("POLLUTANTCODE") == "003") {
                    l3.add(stringObjectMap.get("REVISEDFLOW"));
                }
            }

            JSONArray dust = new JSONArray();
            dust.addAll(l1);
            JSONArray so2 = new JSONArray();
            so2.addAll(l2);
            JSONArray nos = new JSONArray();
            nos.addAll(l3);

            JSONObject last7day = new JSONObject();
            last7day.put("dust", dust);
            last7day.put("so2", so2);
            last7day.put("nos", nos);

            obj.put("psname", psname);
            obj.put("pscode", pscode);
            obj.put("lon", lon);
            obj.put("lat", lat);
            obj.put("psclass", psclass);
            obj.put("attention", attention);
            obj.put("industry", industry);
            obj.put("unit", unit);
            obj.put("region", region);
            obj.put("pollutants", pollutants);
            obj.put("city", city);
            obj.put("if30", if30);
            obj.put("area", area);
            obj.put("last7day", last7day);

        }
        return obj.toJSONString();
    }
}